package com.sdy.dcsb.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.EsbApp;
import com.sdy.dcsb.biz.service.EsbAppService;
import com.sdy.dcsb.biz.service.UserOperationLogService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Objects;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 总线实例
 * </p>
 *
 * @author lx
 * @since 2019-09-27
 */
@Slf4j
@RestController
@RequestMapping("/esbApp")
@Api(tags = "总线实例相关接口")
public class EsbAppController extends BaseController {
    @Autowired
    private EsbAppService esbAppService;
    @Autowired
    private UserOperationLogService userOperationLogService;

    /**
     * 删除数据
     */
    @GetMapping("/delete")
    public Response delete(HttpServletRequest request, Integer id, Integer status) {
        if(isEmpty(id) || isEmpty(status)) {
            return Response.error("请求参数不能为空");
        }
        if (status == 0) {
            userOperationLogService.savaUserLog(request, "删除总线实例服务信息：" + esbAppService.getById(id).getAppname());
            return Response.success(esbAppService.removeById(id));
        }
        return Response.error("在用不能删除");
    }

    @GetMapping("/selectOneMess")
    @ApiOperation("单条信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前id"),
    })
    public Response selectOneMess(Integer id) {
        if(!isEmpty(id)) {
            return Response.success(esbAppService.getById(id));
        }else {
            return Response.error("请求id不能为空");
        }
    }

    @GetMapping("/saveInterface")
    @ApiOperation("新建,修改信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前id"),
            @ApiImplicitParam(dataType = "String", name = "domainname", value = "域名"),
            @ApiImplicitParam(dataType = "String", name = "appname", value = "应用名"),
            @ApiImplicitParam(dataType = "String", name = "ip", value = "IP地址"),
            @ApiImplicitParam(dataType = "String", name = "httpport", value = "http端口"),
            @ApiImplicitParam(dataType = "String", name = "socketport", value = "socket端口"),
            @ApiImplicitParam(dataType = "String", name = "apptype", value = "应用类型"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "状态"),
    })
    public Response saveInterface(HttpServletRequest request, Integer id, String domainname, String appname, String ip,
                                  String httpport, String socketport, String apptype, String status) {
        try {
            if(!isEmpty(domainname)) {
                domainname = URLDecoder.decode(domainname,"utf-8");
            }
            if(!isEmpty(appname)) {
                appname = URLDecoder.decode(appname,"utf-8");
            }
        }catch(IOException e) {

        }
        EsbApp esbApp = new EsbApp();
        esbApp.setDomainname(domainname);
        esbApp.setAppname(appname);
        esbApp.setApptype(apptype);
        esbApp.setHttpport(httpport);
        esbApp.setIp(ip);
        esbApp.setSocketport(socketport);
        esbApp.setStatus(status);
        if (isEmpty(id)){
            userOperationLogService.savaUserLog(request, "新增总线实例服务信息：" + appname);
            return Response.success(esbAppService.save(esbApp));
        }
        EsbApp app = esbAppService.getById(id);
        if (app.getAppname().equals(appname) && app.getDomainname().equals(domainname) &&
                app.getHttpport().equals(httpport) && app.getIp().equals(ip) && app.getSocketport().equals(socketport) && Objects.equals(app.getApptype(), apptype) && Objects.equals(app.getStatus(), status)){
            return Response.error("信息无变化");
        }
        userOperationLogService.savaUserLog(request, "修改总线实例服务信息：" + esbAppService.getById(id).getAppname());
        return Response.success(esbAppService.update(esbApp,new UpdateWrapper<EsbApp>().eq("id",id)));
    }

    @GetMapping("/selectLikeMess")
    @ApiOperation("查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "ip", value = "IP地址"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "状态,1：在用，0：不用 2：查全部"),
            @ApiImplicitParam(dataType = "String", name = "apptype", value = "应用类型 0：查全部 1:服务注册中心,2:服务运行中心,3:服务管理中心.4:服务开发中心,5:原子服务运行中心"),
    })
    public Response selectLikeMess(String ip, String status,String apptype,Page<EsbApp> page) {
        QueryWrapper<EsbApp> wrapper = new QueryWrapper<>();
        if (!isEmpty(ip)) {
            wrapper.like("ip",ip);
        }if (!isEmpty(status) && !status.equals("2")){
            wrapper.like("status",status);
        }if (!isEmpty(apptype) && !apptype.equals("0")){
            wrapper.like("apptype",apptype);
        }
        wrapper.select();
        IPage<EsbApp> result = esbAppService.page(page,wrapper);
        return Response.success(result);
    }

    /**
     * 监控平台 首页展示
     */
    @GetMapping("/firstPage")
    @ApiOperation("展示首页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "apptype", value = "应用类型"),
    })
    public Response firstPage(String apptype) {
        if(!isEmpty(apptype)) {
            QueryWrapper<EsbApp> wrapper = new QueryWrapper<>();
            wrapper.eq("apptype",apptype);
            return Response.success(esbAppService.list(wrapper));
        }else {
            return Response.error("请求的应用类型不能为空");
        }
    }

}